package com.kamistoat.meimeistore.auth.service;

import com.kamistoat.meimeistore.apisystem.domain.SysLogininfor;
import com.kamistoat.meimeistore.apisystem.feign.RemoteLogService;
import com.kamistoat.meimeistore.commoncore.constant.Constants;
import com.kamistoat.meimeistore.commoncore.constant.SecurityConstants;
import com.kamistoat.meimeistore.commoncore.utils.StringUtils;
import com.kamistoat.meimeistore.commoncore.utils.ip.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 记录日志服务组件
 */
@Component
public class SysRecordLogService {
    @Autowired
    RemoteLogService remoteLogService;

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  信息
     */
    public void recordLogininfor(String username, String status, String message) {
        SysLogininfor sysLogininfor = new SysLogininfor();
        sysLogininfor.setUserName(username);
        sysLogininfor.setIpaddr(IpUtils.getIpAddr());
        sysLogininfor.setMsg(message);
        sysLogininfor.setAccessTime(new Date());
        // 日志状态
        if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
            sysLogininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
        } else if (status.equals(Constants.LOGIN_FAIL)) {
            sysLogininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
        }
        // 调用feign接口
        remoteLogService.saveLogininfor(sysLogininfor, SecurityConstants.INNER);
    }


}























